<article class="" id="post-">
<header class="entry-header">
<h1 class="entry-title">jQuery.inArray()</h1>
<div class="entry-meta">
						所属分类：<span class="category"><a href="/category/utilities/">工具类</a></span>
<span class="pull-right">英文文档：<a href="https://api.jquery123.com/jQuery.inArray/" target="_blank">jQuery.inArray()</a></span>
</div><!-- .entry-meta -->
</header><!-- .entry-header -->
<!-- .entry-header -->
<div class="entry-content">
<article class="entry method" id="jQuery-inArray1"><h2 class="section-title">
<span class="name">jQuery.inArray( value, array [, fromIndex ] )</span><span class="returns">返回: <a href="/Types/#Number">Number</a></span>
</h2>
<div class="entry-wrapper">
<p class="desc"><strong>描述: </strong>在数组中查找指定值并返回它的索引（如果没有找到，则返回-1）。</p>
<ul class="signatures"><li class="signature" id="jQuery-inArray-value-array-fromIndex">
<h4 class="name">
<span class="version-details">添加的版本: <a href="/category/version/1.2/">1.2</a></span>jQuery.inArray( value, array [, fromIndex ] )</h4>
<ul>
<li>
<div><strong>value</strong></div>
<div>类型: <a href="/Types/#Anything">Anything</a>
</div>
<div>要查找的值。</div>
</li>
<li>
<div><strong>array</strong></div>
<div>类型: <a href="/Types/#Array">Array</a>
</div>
<div>一个数组，通过它来查找。</div>
</li>
<li>
<div><strong>fromIndex</strong></div>
<div>类型: <a href="/Types/#Number">Number</a>
</div>
<div>数组索引值，表示从哪里在开始查找。默认值是0，这将查找整个数组。</div>
</li>
</ul>
</li></ul>
<div class="longdesc" id="entry-longdesc">
<p><code>$.inArray()</code>方法类似于JavaScript的原生<code>.indexOf()</code>方法，没有找到匹配元素时它返回-1。如果数组第一个元素匹配<code>value</code>（参数） ，那么<code>$.inArray()</code>返回0。</p>
<p>因为JavaScript将0视为false(即 0 == false, 但是 0 !== false)，如果我们检查在<code>array</code>中存在<code>value</code>，我们只需要检查它是否不等于（或大于）-1。</p>
</div>
<section class="entry-examples" id="entry-examples"><header><h2 class="underline">例子:</h2></header><div class="entry-example" id="example-0">
<h4><span class="desc">返回数组中指定元素的索引值。</span></h4>
<div class="syntaxhighlighter xml ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
<div class="line n6">6</div>
<div class="line n7">7</div>
<div class="line n8">8</div>
<div class="line n9">9</div>
<div class="line n10">10</div>
<div class="line n11">11</div>
<div class="line n12">12</div>
<div class="line n13">13</div>
<div class="line n14">14</div>
<div class="line n15">15</div>
<div class="line n16">16</div>
<div class="line n17">17</div>
<div class="line n18">18</div>
<div class="line n19">19</div>
<div class="line n20">20</div>
<div class="line n21">21</div>
<div class="line n22">22</div>
<div class="line n23">23</div>
<div class="line n24">24</div>
<div class="line n25">25</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code><span class="doctype">&lt;!DOCTYPE html&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">html</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">style</span>&gt;</span><span class="css"></span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">div</span> <span class="rules">{ <span class="rule"><span class="attribute">color</span>:<span class="value">blue;</span></span> <span class="rule">}</span></span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">span</span> <span class="rules">{ <span class="rule"><span class="attribute">color</span>:<span class="value">red;</span></span> <span class="rule">}</span></span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">style</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">script</span> <span class="attribute">src</span>=<span class="value">"https://code.jquery.com/jquery-latest.js"</span>&gt;</span><span class="javascript"></span><span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">div</span>&gt;</span>"John" found at <span class="tag">&lt;<span class="title">span</span>&gt;</span><span class="tag">&lt;/<span class="title">span</span>&gt;</span><span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">div</span>&gt;</span>4 found at <span class="tag">&lt;<span class="title">span</span>&gt;</span><span class="tag">&lt;/<span class="title">span</span>&gt;</span><span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">div</span>&gt;</span>"Karl" not found, so <span class="tag">&lt;<span class="title">span</span>&gt;</span><span class="tag">&lt;/<span class="title">span</span>&gt;</span><span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">div</span>&gt;</span>"Pete" is in the array, but not at or after index 2, so <span class="tag">&lt;<span class="title">span</span>&gt;</span><span class="tag">&lt;/<span class="title">span</span>&gt;</span><span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">script</span>&gt;</span><span class="javascript"><span class="keyword">var</span> arr = [ <span class="number">4</span>, <span class="string">"Pete"</span>, <span class="number">8</span>, <span class="string">"John"</span> ];</span></code></div></div><div class="container"><div class="line"><code><span class="keyword">var</span> $spans = $(<span class="string">"span"</span>);</code></div></div><div class="container"><div class="line"><code>$spans.eq(<span class="number">0</span>).text(jQuery.inArray(<span class="string">"John"</span>, arr));</code></div></div><div class="container"><div class="line"><code>$spans.eq(<span class="number">1</span>).text(jQuery.inArray(<span class="number">4</span>, arr));</code></div></div><div class="container"><div class="line"><code>$spans.eq(<span class="number">2</span>).text(jQuery.inArray(<span class="string">"Karl"</span>, arr));</code></div></div><div class="container"><div class="line"><code>$spans.eq(<span class="number">3</span>).text(jQuery.inArray(<span class="string">"Pete"</span>, arr, <span class="number">2</span>));</code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">html</span>&gt;</span></code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<h4>Demo:</h4>
<div class="demo code-demo"></div>
</div></section>
</div></article> </div>
</article>